#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include "model/commonModel/songmodel.h"
#include "normalplaylistmodel.h"
#include "topview/playbarview.h"
#include "playEngine/ezvoiceplayermanager.h"
#include "playListView/playlistview.h"
#include <QSplitter>
#include "config.h"
#include "setting/settingdialogview.h"


MainWindow* MainWindow::shareWindow()
{
    static MainWindow *window = 0;
    if(window==0){
        window = new MainWindow();
    }
    return window;
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    Config::defaultConfig();

    ui->setupUi(this);
    setUnifiedTitleAndToolBarOnMac(true);

    QMenu *m = menuBar()->addMenu(tr("settings"));
    QAction *action = m->addAction(tr("baseSetting"));
    connect(action,SIGNAL(triggered()),this,SLOT(slot_settings()));

    _barView = new PlayBarView(this);
    _barView->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
//    centerWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
//    this->setWindowFlags(Qt::WindowFlags);
    _barView->setObjectName(QString("_barView"));
    _barView->setStyleSheet(QString("#_barView{}"));
//    this->s

    QToolBar *toolBar = addToolBar(tr("mainWindow-toolBar-name"));
    toolBar->addWidget(_barView);
    toolBar->setMovable(false);
    toolBar->setMinimumHeight(65);
    toolBar->setMaximumHeight(65);
    toolBar->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);


    splitter = new QSplitter(Qt::Horizontal,this);
    ui->mainLayout->addWidget(splitter);



    PlayListView *playListVew = new PlayListView(splitter);
    connect(playListVew,SIGNAL(sig_changeCenterWidget(QWidget*)),this,SLOT(slot_changeCenterWidget(QWidget*)));

    centerWidget = new QWidget(splitter);
    QHBoxLayout *centerWidgetLayout = new QHBoxLayout(centerWidget);
    centerWidget->setLayout(centerWidgetLayout);

    splitter->addWidget(playListVew);
    splitter->addWidget(centerWidget);
    QList<int> sizes;
    sizes << 10000 << 30000;
    splitter->setSizes(sizes);

    currentWidget = 0;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slot_changeCenterWidget(QWidget *widget)
{
    if(currentWidget){
        currentWidget->hide();
        centerWidget->layout()->removeWidget(currentWidget);
        EZTally::EZTallyObject *obj = dynamic_cast<EZTally::EZTallyObject*>(currentWidget);
        if(obj){
            obj->Release();
        }else{
            delete currentWidget;
        }

    }
    {
        EZTally::EZTallyObject *obj = dynamic_cast<EZTally::EZTallyObject*>(widget);
        if(obj){
            obj->Retain();
        }

    }
    widget->show();
    centerWidget->layout()->addWidget(widget);
    currentWidget = widget;
//    widget->repaint();
}

void MainWindow::slot_settings()
{
    SettingDialogView sdv(this);
    sdv.exec();
}
